home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / pascal / bitmbtn.exe / BUTTONS.PAS < prev    next >
Pascal/Delphi Source File  |  1991-11-13  |  14KB  |  368 lines

  1. Unit Buttons;
  2. {***************************************************************************}
  3. {*  Buttons.pas  by Daniel Thomas (CIS: 72301,2164)                        *}
  4. {*                                                                         *}
  5. {*    This code is hereby donated to tbe Public Domain.  Have fun!         *}
  6. {*                                                                         *}
  7. {*                                                                         *}
  8. {* There are 2 kinds of Button objects in this library.  Both of them      *}
  9. {* are for creating buttons made up of bitmaps.  The first object uses     *}
  10. {* only one bitmap, and draws any needed "effects".  The second object     *}
  11. {* uses three bitmaps, one for each state (up, down, and disabled).        *}
  12. {*                                                                         *}
  13. {* Neither of these objects supports drawing on Dialogs, but it's a        *}
  14. {* start!  Also, neither of these buttons looks any different if it's      *}
  15. {* the default button.  Again, you're on your own!                         *}
  16. {*                                                                         *}
  17. {* For either type of button, just Init the object in the parent's Init    *}
  18. {* method, and you've got a button!  If you want to create it in some      *}
  19. {* other method than the parent's Init method, use Application^.MakeWindow *}
  20. {* to make it visible.                                                     *}
  21. {*                                                                         *}
  22. {* Add a method to the parent window, like this:                           *}
  23. {*                                                                         *}
  24. {*    Procedure wmDrawItem(var Msg:tMessage);virtual wm_First+wm_DrawItem; *}
  25. {*                                                                         *}
  26. {* It should look like this:                                               *}
  27. {*                                                                         *}
  28. {*    Procedure tMainWindow.wmDrawItem(var Msg:tMessage);                  *}
  29. {*    begin                                                                *}
  30. {*      with pDrawItemStruct(Msg.lParam)^ do                               *}
  31. {*        case CtlType of                                                  *}
  32. {*            odt_Button:                                                  *}
  33. {*              case CtlID of                                              *}
  34. {*                  id_Button1 : Button1^.DrawItem(Msg);                   *}
  35. {*                  id_Button2 : Button2^.DrawItem(Msg);                   *}
  36. {*              end;                                                       *}
  37. {*        end;                                                             *}
  38. {*    end;                                                                 *}
  39. {*                                                                         *}
  40. {*                                                                         *}
  41. {* tSingleBitmapButton                                                     *}
  42. {*                                                                         *}
  43. {*   Two Init methods.  The second one allows you to specify the color     *}
  44. {*   of a surrounding box.                                                 *}
  45. {*                                                                         *}
  46. {*   Specify the parent object (@self), an ID number, the location of      *}
  47. {*   the button (x & y), whether it's the default, and the name of the     *}
  48. {*   bitmap.  (The second Init method also let's you specify an RGB        *}
  49. {*   color for a border).                                                  *}
  50. {*                                                                         *}
  51. {*   Create the bitmap as large as the button needs to be, minus the       *}
  52. {*   surrounding black box.  It is assumed that the button's "background"  *}
  53. {*   color is light gray.  The "shading" effects will be drawn for you.    *}
  54. {*                                                                         *}
  55. {* tMultiBitmapButton                                                      *}
  56. {*                                                                         *}
  57. {*   Specify the parent object (@self), an ID number, the location of      *}
  58. {*   the button (x & y), whether it's the default, and the name of the     *}
  59. {*   three bitmaps (one for a non-pressed button, one for a pressed        *}
  60. {*   button, and one for a disabled button).  ALL THREE MUST BE THE SAME   *}
  61. {*   SIZE.                                                                 *}
  62. {*                                                                         *}
  63. {***************************************************************************}
  64.  
  65. interface
  66.  
  67. uses WinTypes,WinProcs,WObjects;
  68.  
  69. type
  70.   pSingleBitmapButton=^tSingleBitmapButton;
  71.   tSingleBitmapButton=object(tButton)
  72.       Bitmap             : hBitmap;
  73.       UseSpecialBorder   : boolean;
  74.       SpecialBorderColor : longint;
  75.  
  76.       constructor Init(aParent: pWindowsObject; aID: Integer;
  77.                        X,Y: Integer; IsDefault: Boolean;
  78.                        aBitmap: pChar);
  79.       constructor InitWithSpecialBorder(
  80.                        aParent: pWindowsObject; aID: Integer;
  81.                        X,Y: Integer; IsDefault: Boolean;
  82.                        aBitmap: pChar;
  83.                        aSpecialBorderColor: longint);
  84.       destructor Done; virtual;
  85.       procedure DrawItem(var Msg:tMessage); virtual;
  86.     end;
  87.  
  88.   pMultiBitmapButton=^tMultiBitmapButton;
  89.   tMultiBitmapButton=object(tButton)
  90.       NormalBitmap,
  91.       DownBitmap,
  92.       DisabledBitmap : hBitmap;
  93.  
  94.       constructor Init(aParent: pWindowsObject; aID: Integer;
  95.                        X,Y: Integer; IsDefault: Boolean;
  96.                        aNormalBitmap,aDownBitmap,aDisabledBitmap: pChar);
  97.       destructor Done; virtual;
  98.       procedure DrawItem(var Msg:tMessage); virtual;
  99.     end;
  100.  
  101. implementation
  102.  
  103. const
  104.   cBlackColor     = $00000000;
  105.   cWhiteColor     = $00FFFFFF;
  106.   cDarkGrayColor  = $00808080;
  107.   cLightGrayColor = $00C0C0C0;
  108.  
  109. type
  110.   pDrawItemStruct = ^tDrawItemStruct;
  111.  
  112. var
  113.   bmp : hBitmap;
  114.  
  115. {**************************************************************************}
  116. {*  tSingleBitmapButton                                                   *}
  117. {**************************************************************************}
  118.  
  119. constructor tSingleBitmapButton.Init(aParent: pWindowsObject; aID: Integer;
  120.                                      X,Y: Integer; IsDefault: Boolean;
  121.                                      aBitmap: pChar);
  122.  
  123. var
  124.   bm  : tBitMap;
  125.   w,h : integer;
  126.  
  127. begin
  128.   bmp := LoadBitmap(hInstance,aBitmap);
  129.   GetObject(bmp,sizeof(bm),@bm);
  130.   tButton.Init(aParent,aID,'Dummy',x,y,bm.bmWidth+2,bm.bmHeight+2,IsDefault);
  131.   Attr.Style := Attr.Style or bs_OwnerDraw;
  132.   Bitmap := bmp;
  133.   UseSpecialBorder := false;
  134. end; {tSingleBitmapButton.Init}
  135.  
  136. constructor tSingleBitmapButton.InitWithSpecialBorder(
  137.                                      aParent: pWindowsObject; aID: Integer;
  138.                                      X,Y: Integer; IsDefault: Boolean;
  139.                                      aBitmap: pChar;
  140.                                      aSpecialBorderColor: longint);
  141.  
  142. var
  143.   bm  : tBitMap;
  144.   w,h : integer;
  145.  
  146. begin
  147.   bmp := LoadBitmap(hInstance,aBitmap);
  148.   GetObject(bmp,sizeof(bm),@bm);
  149.   tButton.Init(aParent,aID,'Dummy',x,y,bm.bmWidth+4,bm.bmHeight+4,IsDefault);
  150.   Attr.Style := Attr.Style or bs_OwnerDraw;
  151.   Bitmap := bmp;
  152.   UseSpecialBorder := true;
  153.   SpecialBorderColor := aSpecialBorderColor;
  154. end; {tSingleBitmapButton.Init}
  155.  
  156. destructor tSingleBitmapButton.Done;
  157.  
  158. begin
  159.   tButton.Done;
  160.   DeleteObject(Bitmap);
  161. end; {tSingleBitmapButton.Done}
  162.  
  163. procedure tSingleBitmapButton.DrawItem(var Msg: tMessage);
  164.  
  165. var
  166.   Down            : boolean;
  167.   OldMode,
  168.   w,h,x1,y1       : integer;
  169.   MemDC           : hDC;
  170.   OldBmp          : hBitmap;
  171.   Pts             : array[0.